package dao.fixed;


import model.Film;
import dao.AbstractRecordDAO;



public class FilmFixedRecordDAO extends AbstractRecordDAO<Film>
{

  @Override
  protected String build(Film film) 
  {
    return String.format("%-4d%-20s%-30s%-20s%3d", film.getId(), film.getName().trim(), film.getHauptdarsteller().trim(), film.getGenre(), film.getLaenge());
  }

  @Override
  protected Film parse(String line) 
  {
    if (line.trim().length() != 77) 
    { 
    	throw new IllegalArgumentException(String.format("wrong format in line: %s", line)); 
    }

    try 
    {
      Long id = Long.valueOf(line.substring(0, 4).trim());
      String name = line.substring(4, 24).trim();
      String hauptdarsteller = line.substring(24, 54).trim();
      String genre = line.substring(54, 74).trim();
      int laenge = Integer.valueOf(line.substring(74, 77).trim());
      return new Film(id, name, hauptdarsteller, genre, laenge);
    }
    catch (NumberFormatException e) 
    {
      throw new IllegalArgumentException(String.format("wrong format in laenge (%s)", line.substring(70, 77)), e);
    }
  }
}